package com.wenhai.leetcode.linkedList;

/**
 * @ClassName _21合并两个有序链表
 * @Description: https://leetcode-cn.com/problems/merge-two-sorted-lists/
 * @Author 谢文海
 * @Date 2021/4/5 19:32
 * @Version 1.0
 **/
public class _21合并两个有序链表 {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null) {
            return l2;
        }
        if (l2 == null) {
            return l1;
        }
        ListNode dummyListNode = new ListNode();
        ListNode cur = dummyListNode;
        while (l1 != null && l2 != null) {
            if (l2.val > l1.val) {
                cur.next = l1;
                l1 = l1.next;
            } else {
                cur.next = l2;
                l2 = l2.next;
            }
            cur = cur.next;
        }
        cur.next = l1 == null ? l2 : l1;
        return dummyListNode.next;
    }


    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

}
